package com.gange.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gange.entity.Favorite;
import com.gange.entity.User;
import com.gange.service.IFavoriteService;
import com.gange.service.IUserService;
import com.gange.utils.JSONUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
public class UserServiceController {
    @Autowired
    private IUserService userService;
    @Autowired
    private IFavoriteService favoriteService;
    //根据用户id返回用户信息服务
    @GetMapping("/userInfo/{id}")
    public User user(@PathVariable("id") String id) {
        log.info("接收到用户id: {}", id);
        User user = userService.getById(Integer.valueOf(id));
        log.info("返回的用户信息: {}", JSONUtils.writeValueAsString(user));
        return user;
    }
    /**
     * 根据用户id视频id查询是否收藏
     *
     * @param videoId
     * @param userId
     * @return
     */
    @GetMapping("/userInfo/favorite")
    public Favorite favorite(@RequestParam("videoId") String videoId, @RequestParam("userId") String userId) {
        log.info("接收到的视频id {}, 用户id: {}", videoId, userId);
        QueryWrapper<Favorite> wrapper = new QueryWrapper<>();
        wrapper.eq("id",videoId).eq("uid",userId);
        Favorite favorite = favoriteService.getOne(wrapper);
        log.info("当前返回的收藏对象是否为空: {}", ObjectUtils.isEmpty(favorite));
        return favorite;
    }
}
